<?php

class Ead_CompraController extends App_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction() {
        
        Zend_Loader::loadClass('datetools','library');
        Zend_Loader::loadClass('tools','library');
        Zend_Loader::loadClass('compras','library');
        
        $this->view->status = compras::returnStatus();
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/curso.js');
        
        $search = $this->_getParam('inp-search', '');
        $this->view->search = $search;
        $where = 'COM_ID > 0';
        if(strlen($search) > 1){
            //$where .= ' and COM_ like "%'.$search.'%"';
        }
        
        $COM_STATRA = $this->_getParam('COM_STATRA', '');
        if(strlen($COM_STATRA) > 0) $where .= ' and COM_STATRA = "'.$COM_STATRA.'"';
        
        $obj = new Ead_Model_DbTable_Compra();
        $result = $obj->fetchAll($obj->select()->where($where)->order('COM_DATHOR DESC'));
        $this->view->result = $this->paginacao($result);
        
 
    }
    
    
    public function editAction() {
        Zend_Loader::loadClass('tools','library');
        Zend_Loader::loadClass('datetools','library');
        Zend_Loader::loadClass('compras','library');
        
        $form = new Ead_Form_Compra();
        
        $this->view->form = $form;
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                $id = (int) $form->getValue('COM_ID');
                $data = array(
                    'COM_ID'     => $form->getValue('COM_ID'),
                    'COM_STATRA' => $form->getValue('COM_STATRA'),
                    'COM_DATPAG' => Datetools::dma_amd($form->getValue('COM_DATPAG')),
                );
                $obj = new Ead_Model_DbTable_Compra();
                    
                if ($id == 0)
                    $obj->insert($data);
                else
                    $obj->update($data, 'COM_ID = ' . $id);

                $this->_helper->redirector('index');
            } else {
                $form->populate($formData);
            }
        } else {
            $id = $this->_getParam('id', 0);
            if ($id > 0) {
                $obj = new Ead_Model_DbTable_Compra();
                $row = $obj->fetchRow('COM_ID = ' . $id);
                
                $cur = $row->findDependentRowSet("Ead_Model_DbTable_Curso");
                $usu = $row->findDependentRowSet("Ead_Model_DbTable_Usuarios");
                
                $row['COM_DATHOR'] = Datetools::invertDH($row['COM_DATHOR']);
                
                $row = $row->toArray();
                
                $row['CUR_NOM'] = $cur[0]['CUR_NOM'];
                $row['USU_NOME'] = $usu[0]['USU_NOME'];
                $row['COM_VLR'] = tools::float_to_money($row['COM_VLR']);
                $row['COM_DES'] = tools::float_to_money($row['COM_DES']);
                $row['COM_DATPAG'] = Datetools::amd_dma($row['COM_DATPAG']); 
                $form->populate($row);
            }
        }
    }

    public function deleteAction() {
        
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        
        $ok = FALSE;
        
        $id = $this->_getParam('id', 0);
        if ((int)$id > 0 ) {
            $obj = new Ead_Model_DbTable_Compra();
            $ok = (bool) $obj->delete('COM_ID = '.(int)$id);
            //$this->_helper->redirector('index'); 
        } else {
            //$id = $this->_getParam('id', 0);
            //$obj = new Ead_Model_DbTable_Compra();
            //$form->populate($obj->fetchRow('COM_ID = ' . $id)->toArray());
        }
        
        echo json_encode(array('ok' => $ok));
    }

}

